import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// Tdesign自动导入和组件导入插件
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { TDesignResolver } from '@tdesign-vue-next/auto-import-resolver';

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [
        TDesignResolver({
          library: "vue-next",
        }),
      ],
    }),
    Components({
      resolvers: [
        TDesignResolver({
          library: "vue-next",
        }),
      ],
    }),
  ],

  // 设置开发服务器配置
  server: {
    host: "0.0.0.0",
    port: 8080,
    open: true,
    proxy: {
      "/api": {
        target: "http://localhost:6060", // API网关地址
        changeOrigin: true,
        // 不重写路径，保留/api前缀，因为网关期望完整的/api/v1/auth/login路径
        // rewrite: (path) => path.replace(/^\/api/, ""),
        // 开发环境下的额外配置
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('proxy error', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Sending Request to the Target:', req.method, req.url);
          });
        }
      },
    },
  },

  // 配置路径别名
  resolve: {
    alias: [
      {
        find: "@",
        replacement: "/src",
      },
    ],
  },

  // CSS预处理器配置
  css: {
    preprocessorOptions: {
      scss: {
        // 使用现代Sass API，避免弃用警告
        api: 'modern',
        // 静默弃用警告
        silenceDeprecations: ['legacy-js-api'],
        // 全局样式变量导入（如果需要）
        additionalData: `@use "sass:math";`,
      },
    },
  },
});
